namespace EyeSoft.Wpf.Facilities
{
	using System;
	using System.Windows;
	using System.Windows.Threading;

	public class SyncExecution : Execution
	{
		internal SyncExecution(Application application)
			: base(application, false)
		{
		}

		public T Execute<T>(Func<T> func)
		{
			return Execute(func, DispatcherPriority.Normal);
		}

		public T Execute<T>(Func<T> func, DispatcherPriority priority)
		{
			if (application != null)
			{
				var dispatcher = application.Dispatcher;

				if (dispatcher.CheckAccess())
				{
					return func();
				}

				return (T)dispatcher.Invoke(func, priority);
			}

			return func();
		}
	}
}